1 package org.smartcomps.twister.engine.core.dynamic; 2 3 import junit.framework.TestCase; 4 import net.sf.hibernate.tool.hbm2ddl.SchemaExport; 5 import net.sf.hibernate.cfg.Configuration; 6 import org.smartcomps.twister.common.transaction.TransactionManager; 7 import org.smartcomps.twister.common.lifecycle.LifecycleManager; 8 import org.smartcomps.twister.engine.priv.core.definition.*; 9 import org.smartcomps.twister.engine.priv.core.dynamic.PickEC; 10 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstance; 11 import org.smartcomps.twister.engine.priv.core.dynamic.ProcessInstanceFactory; 12 import org.smartcomps.twister.engine.priv.messaging.impl.DefaultMessageBrokerImpl; 13 import org.smartcomps.twister.engine.TwisterEngineFactory; 14 import org.smartcomps.twister.deployer.TwisterDeployer; 15 import org.smartcomps.twister.deployer.TwisterDeployerFactory; 16 import org.smartcomps.twister.util.BeanTester; 17 import org.dom4j.Document; 18 import org.dom4j.DocumentHelper; 19 import org.dom4j.Element; 20 21 import java.util.HashSet; 22 import java.util.Calendar; 23 import java.util.HashMap; 24 25 import com.sun.msv.datatype.xsd.DatatypeFactory; 26 27 public class TestPickEC extends TestCase { 28 29 private TwisterDeployer deployer; 30 31 private TwisterProcess process; 32 private Pick pick; 33 private Invoke invoke1; 34 private Invoke invoke2; 35 private Invoke invoke3; 36 37 protected void setUp() throws Exception { 38 SchemaExport schemaExport = new SchemaExport(new Configuration().configure()); 39 schemaExport.create(true, true); 40 41 LifecycleManager.getLifecycleManager().createResources(); 42 LifecycleManager.getLifecycleManager().startResources(); 43 44 deployer = TwisterDeployerFactory.getTwisterDeployer(); 45 } 46 47 protected void tearDown() throws Exception { 48 LifecycleManager.getLifecycleManager().stopResources(); 49 LifecycleManager.getLifecycleManager().destroyResources(); 50 } 51 52 public void testDirectExecute() throws Exception { 53 System.out.println("######################################## testDirectExecute"); 54 TransactionManager.beginTransaction(); 55 createPick(); 56 57 HashMap corrMap = new HashMap(); 58 corrMap.put("mainProp", "MP21"); 59 pick.execute("mainCorr", corrMap); 60 61 Document doc = DocumentHelper.createDocument(); 62 Element part = doc.addElement("message").addElement("main"); 63 part.addElement("orderid").setText("217"); 64 part.addElement("customerid").setText("E24F56"); 65 ((PickEC)pick.getExecutionContexts().iterator().next()).acknowledgeMessage(invoke2, doc); 66 67 ProcessInstance createdInstance = ProcessInstanceFactory.findInstanceByCorrelation("mainCorr", corrMap); 68 assertNotNull("Could not find any instance created with specified correlation", createdInstance); 69 assertEquals("Instance is not terminated", ProcessInstance.COMPLETED, createdInstance.getStatus()); 70 assertNotNull("Correct invoke has not been executed", 71 DefaultMessageBrokerImpl.getMessage("partner2", "porttype2", "operation2")); 72 TransactionManager.commitTransaction(); 73 } 74 75 public void testAlarm() throws Exception { 76 TransactionManager.beginTransaction(); 77 createPick(); 78 79 HashMap corrMap = new HashMap(); 80 corrMap.put("mainProp", "MP21"); 81 pick.execute("mainCorr", corrMap); 82 83 ProcessInstance createdInstance = ProcessInstanceFactory.findInstanceByCorrelation("mainCorr", corrMap); 84 assertEquals("Process is not active after execution", ProcessInstance.ACTIVE, createdInstance.getStatus()); 85 86 TransactionManager.commitTransaction(); 87 Thread.sleep(70000); 88 TransactionManager.beginTransaction(); 89 90 createdInstance = ProcessInstanceFactory.findInstanceByCorrelation("mainCorr", corrMap); 91 assertEquals("Process is not completed after execution ended", ProcessInstance.COMPLETED, createdInstance.getStatus()); 92 assertNotNull("Correct invoke has not been executed", 93 DefaultMessageBrokerImpl.getMessage("partner3", "porttype3", "operation3")); 94 TransactionManager.commitTransaction(); 95 } 96 97 public void testSimplePickWithDescription() throws Exception { 98 System.out.println("######################################## testSimplePickWithDescription"); 99 deployer.deploy(getClass().getClassLoader().getResource("test-pick.xml")); 100 101 // Starting the second onMessage in test pick. 102 Document doc = DocumentHelper.createDocument(); 103 doc.addElement("message").addElement("main").addElement("pickid") 104 .setText((String) BeanTester.generateRandomValue(String.class)); 105 int result = TwisterEngineFactory.getEngine().acknowledge("secPartner", "secPort", "secOp", doc); 106 assertEquals("A problem occured when the message as been sent", 0, result); 107 108 Thread.sleep(10000); 109 110 Document receivedDoc = DefaultMessageBrokerImpl.getMessage("somePartner", "somePort", "someOp"); 111 assertNotNull("Document received ", receivedDoc); 112 } 113 114 public void testDoublePickWithDescription() throws Exception { 115 System.out.println("######################################## testDoublePickWithDescription"); 116 deployer.deploy(getClass().getClassLoader().getResource("test-pick.xml")); 117 118 // Starting the first onMessage in test pick. 119 Document doc = DocumentHelper.createDocument(); 120 doc.addElement("message").addElement("main").addElement("pickid") 121 .setText((String) BeanTester.generateRandomValue(String.class)); 122 int result = TwisterEngineFactory.getEngine().acknowledge("outPartner", "outPort", "outOp", doc); 123 assertEquals("A problem occured when the message as been sent", 0, result); 124 125 Thread.sleep(10000); 126 127 result = TwisterEngineFactory.getEngine().acknowledge("inPartner", "inPort", "inOp", doc); 128 assertEquals("A problem occured when the message as been sent", 0, result); 129 130 Thread.sleep(10000); 131 132 Document receivedDoc = DefaultMessageBrokerImpl.getMessage("aPartner", "aPort", "anOp"); 133 assertNotNull("Document received ", receivedDoc); 134 } 135 136 public void testDoublePickAlarmWithDescription() throws Exception { 137 System.out.println("######################################## testDoublePickAlarmWithDescription"); 138 deployer.deploy(getClass().getClassLoader().getResource("test-pick.xml")); 139 140 // Starting the first onMessage in test pick. 141 Document doc = DocumentHelper.createDocument(); 142 doc.addElement("message").addElement("main").addElement("pickid") 143 .setText((String) BeanTester.generateRandomValue(String.class)); 144 int result = TwisterEngineFactory.getEngine().acknowledge("outPartner", "outPort", "outOp", doc); 145 assertEquals("A problem occured when the message as been sent", 0, result); 146 147 Thread.sleep(80000); 148 149 Document receivedDoc = DefaultMessageBrokerImpl.getMessage("otherPartner", "otherPort", "otherOp"); 150 assertNotNull("Document received ", receivedDoc); 151 } 152 153 private void createPick() throws Exception { 154 process = ProcessFactory.createProcess("TestProcess"); 155 ProcessFactory.addProperty(process, "corrProp11", "xs:string", "tns:msg1", "main", "/orderid"); 156 ProcessFactory.addProperty(process, "corrProp12", "xs:string", "tns:msg2", "main", "/customerid"); 157 ProcessFactory.addProperty(process, "corrProp21", "xs:string", "tns:msg1", "main", "/shipid"); 158 ProcessFactory.addProperty(process, "corrProp22", "xs:string", "tns:msg2", "main", "/shipperid"); 159 ProcessFactory.addProperty(process, "mainProp", "xs:string", "tns:mainmsg", "main", "/mainid"); 160 ProcessFactory.addCorrelation(process, "correlation1", "corrProp11 corrProp12"); 161 ProcessFactory.addCorrelation(process, "correlation2", "corrProp21 corrProp22"); 162 ProcessFactory.addCorrelation(process, "mainCorr", "mainProp"); 163 164 pick = (Pick) ActivityFactory.createActivity(Pick.class, process); 165 invoke1 = (Invoke) ActivityFactory.createActivity(Invoke.class, pick); 166 invoke1.setName("invoke1"); 167 invoke1.setPartner("partner1"); 168 invoke1.setOperation("operation1"); 169 invoke1.setPortType("porttype1"); 170 invoke2 = (Invoke) ActivityFactory.createActivity(Invoke.class, pick); 171 invoke2.setName("invoke2"); 172 invoke2.setPartner("partner2"); 173 invoke2.setOperation("operation2"); 174 invoke2.setPortType("porttype2"); 175 invoke3 = (Invoke) ActivityFactory.createActivity(Invoke.class, pick); 176 invoke3.setName("invoke3"); 177 invoke3.setPartner("partner3"); 178 invoke3.setOperation("operation3"); 179 invoke3.setPortType("porttype3"); 180 181 CorrelationRef cref1 = ActivityFactory.createCorrelationRef("correlation1", true, CorrelationRef.IN); 182 HashSet cref1List = new HashSet(); 183 cref1List.add(cref1); 184 ActivityFactory.addMessageEvent(pick, invoke1, "link1", "port1", "op1", "var1", cref1List); 185 CorrelationRef cref2 = ActivityFactory.createCorrelationRef("correlation1", true, CorrelationRef.IN); 186 HashSet cref2List = new HashSet(); 187 cref2List.add(cref2); 188 ActivityFactory.addMessageEvent(pick, invoke2, "link2", "port2", "op2", "var2", cref2List); 189 ActivityFactory.addAlarmEvent(pick, invoke3, createDate30SecsLater(), AlarmEvent.DEADLINE_EXPR); 190 } 191 192 private String createDate30SecsLater() throws Exception { 193 Calendar nowPlus30 = Calendar.getInstance(); 194 nowPlus30.add(Calendar.SECOND, 30); 195 String nowPlus30Str = DatatypeFactory.getTypeByName("dateTime").serializeJavaObject(nowPlus30, null); 196 System.out.println("=> " + nowPlus30Str); 197 return nowPlus30Str; 198 } 199 }

This page was automatically generated by Maven